
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ini();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ini()
{
    ui->mainToolBar->addAction(QIcon("://add.png"),tr("ADD"));
    ui->mainToolBar->addSeparator();
    //ui->mainToolBar->addAction(QIcon("://start.png"),tr("START"));
    //ui->mainToolBar->addAction(QIcon("://stop.png"),tr("STOP"));
    //ui->mainToolBar->addSeparator();
    //ui->mainToolBar->addAction(QIcon("://setting.png"),tr("SETTING"));

    connect(ui->mainToolBar, SIGNAL(actionTriggered(QAction *)), this ,SLOT(slotActionTriggered(QAction *)));


    manager = new DownloadManager(this);
    connect(manager, SIGNAL(finished()), this, SLOT(flish()));
    connect(manager, SIGNAL(allProgressMessage(const QString &)), this, SLOT(slotAllProgressMessage(const QString &)));
    connect(manager, SIGNAL(progressMessage(const QString &)), this, SLOT(slotProgressMessage(const QString &)));
}

void MainWindow::add()
{
    fileName = QFileDialog::getOpenFileName(this,
                                            tr("Open Down List"), "", tr("Down List Files (*.txt)"));
    if(fileName == "")
        return;
    rdlist = new readDownlist(fileName);
    manager->append(rdlist->readAll());
}
void MainWindow::start()
{

}

void MainWindow::stop()
{

}

void MainWindow::setting()
{
    sdialog = new settingDialog();
    sdialog->exec();
}

void MainWindow::flish()
{
    QMessageBox::information(NULL,tr("DOWN FLISH"),tr("Down List Flish."), QMessageBox::Yes, QMessageBox::Yes);
    ui->statusBar->showMessage(tr("Down List Flish."));
}

void MainWindow::slotActionTriggered(QAction *action)
{
    QString actionText = action->text();
    if(actionText == tr("ADD"))
        add();
    if(actionText == tr("START"))
        start();
    if(actionText == tr("STOP"))
        stop();
    if(actionText == tr("SETTING"))
        setting();
}

void MainWindow::slotAllProgressMessage(const QString &message)
{
    ui->statusBar->showMessage(message);
}

void MainWindow::slotProgressMessage(const QString &message)
{
    ui->textEdit->append(message);
}
